package cn.elead.chaos.framework.web.serializer;

import java.io.IOException;
import java.util.Date;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import cn.elead.chaos.core.util.DateUtil;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;

/**
 * 将Date转成String返回到前台
 * 
 * @author luopeng
 *
 */
public class DateJsonSerializer extends JsonSerializer<Date> {

	@Override
	public void serialize(Date date, JsonGenerator gen, SerializerProvider serializers) throws IOException {
		if(ObjectUtil.isEmpty(date)) {
			gen.writeString(StrUtil.EMPTY);
			return;
		}
		gen.writeString(DateUtil.format(date, DatePattern.NORM_DATETIME_PATTERN));
	}

}
